<template>
  <div>
    <Table class="events-table charts-border"
           stripe
           :columns="colData"
           :data="showTableData"
           :height="900"
           :max-height="900"
           @on-current-change="handleRowChange"
           @on-row-dblclick="handleTableClick"></Table>
    <Page :total="eventsTotal"
          :current="eventsPageCurrent"
          :page-size="eventsPageSize"
          @on-change="handlePageChange"
          show-elevator
          style="text-align:left;color: white;margin:10px 0px;" />
    <Modal class="table-detail-xxx"
           :styles="{ top: '180px' }"
           v-model="isShowModal"
           :width="modelWidth">
      <p slot="header"
         style="height:30px;line-height:30px;font-size:24px;">
        <span>{{ eventDetailTitle }}</span>
      </p>
      <div style="height: 100%;">
        <Row style="width:100%;height:100%;"
             v-if="showPeople">
          <i-col span="18"
                 style="height:100%;">
            <img v-if="showImgVideo"
                 style="border:15px solid rgba(0,90,181,0.5)"
                 :src="eventDetailData.p_path"
                 width="100%"
                 height="100%" />
            <!-- <Mp4VideoPlayer v-else :src="eventDetailData.v_path"></Mp4VideoPlayer> -->
            <video v-else
                   :src="eventDetailData.v_path"
                   width="100%"
                   height="100%"
                   autoplay
                   controls
                   ref="videoFlag"></video>
          </i-col>
          <i-col span="6">
            <img v-if="showImgVideo"
                 style="border:15px solid rgba(0,90,181,0.5);top:0px;"
                 :src="eventDetailData.b_p_path"
                 width="100%"
                 height="100%" />
          </i-col>
        </Row>
        <div v-else>
          <img style="border:15px solid rgba(0,90,181,0.5)"
               v-if="showImgVideo"
               :src="eventDetailData.p_path"
               width="100%"
               height="100%" />
          <!-- <Mp4VideoPlayer v-else :src="eventDetailData.v_path"></Mp4VideoPlayer> -->
          <video v-else
                 :src="eventDetailData.v_path"
                 width="100%"
                 height="100%"
                 autoplay
                 controls
                 ref="videoFlag"></video>
        </div>
      </div>
      <div slot="footer">
        <Button type="success"
                size="large"
                @click="showEventImg">显示图片</Button>
        <Button v-if="
            eventDetailData.v_path !== '' && eventDetailData.v_path !== 'null'
          "
                type="warning"
                size="large"
                @click="showEventVideo">播放视频</Button>
        <Button type="error"
                size="large"
                v-if="showControlTime"
                @click="toEventTime">跳转到事件发生处</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import { mapState, mapGetters, mapActions } from "vuex";
import Mp4VideoPlayer from "@/components/myvideo/Mp4VideoPlayer";
import saveAs from "file-saver";
var FileSaver = require("file-saver");
export default {
  name: "eventsTable",
  components: {
    Mp4VideoPlayer
  },
  data() {
    return {
      // 弹出 对话框 显示事件详情
      eventDetailData: {},
      isShowModal: false, // 是否显示对话框
      showImgVideo: true, // true 为图片，false 为视频
      showControlTime: false, // 控制进度
      pageTotal: 0,
      pageCurrent: 1,
      pageSize: 20,
      modelWidth: 1080,
      showPeople: false,
      colData: [
        { title: "事件序号", key: "index", width: "150", align: "center" },
        {
          title: "事件截图",
          key: "events_smallpic",
          width: "150",
          align: "center",
          render: (h, params) => {
            return h("img", {
              style: {
                width: "90px",
                height: "70px"
              },
              attrs: {
                src: params.row.p_path
              }
            });
          }
        },
        { title: "报警时间", key: "datetime", width: "300", align: "center" },
        { title: "报警路段", key: "road_name", width: "auto", align: "center" },
        // { title: "事件原因", key: "cause", width: "auto" },
        // { title: "事件级别", key: "level", width: "140" },
        // { title: "事件状态", key: "status", width: "140", align: "center" },
        {
          title: " ",
          key: "events_typeicon",
          width: "140",
          align: "right",
          render: (h, params) => {
            return h("img", {
              style: {
                width: "28px",
                height: "28px"
              },
              attrs: {
                // src: require(('../../assets/' + params.row.event + '.png'),
                src: require("../../assets/" + params.row.event + ".png")
              }
            });
          }
        },
        { title: "报警类型", key: "event", width: "170", align: "left" },
        {
          title: "事件详情",
          key: "detail",
          width: "auto",
          align: "center",
          render: (h, params) => {
            return h(
              "Button",
              {
                props: {
                  type: "success",
                  size: "small"
                },
                style: {
                  marginRight: "5px"
                },
                on: {
                  click: () => {
                    this.eventDetailData = params.row;

                    this.showPeopleImg();
                    this.isShowModal = true;
                    this.showImgVideo = false; // 显示详情，默认打开图片
                    this.showImgVideo = true; // 显示详情，默认打开图片
                    this.showControlTime = false;
                  }
                }
              },
              "查看详情"
            );
          }
        },

        {
          title: "事件视频",
          key: "vidio",
          width: "auto",
          align: "center",
          render: (h, params) => {
            if (params.row.v_path === "null" || params.row.v_path === "")
              return h("Span", {}, "无视频");
            else
              return h(
                "Button",
                {
                  props: {
                    type: "warning",
                    size: "small"
                  },
                  on: {
                    click: () => {
                      this.showPeople = false;
                      this.modelWidth = 1080;
                      this.showImgVideo = true; // 播放视频

                      setTimeout(() => {
                        this.eventDetailData = params.row;
                        this.showControlTime = true;
                        this.isShowModal = true;
                        this.showImgVideo = false; // 播放视频
                      }, 100);
                    }
                  }
                },
                "查看视频"
              );
          }
        },
        {
          title: "下载图片",
          key: "downloadImg",
          width: "auto",
          align: "center",
          render: (h, params) => {
            return h(
              "Button",
              {
                props: {
                  type: "success",
                  size: "small"
                },
                on: {
                  click: () => {
                    // 下载图片代码
                    FileSaver.saveAs(params.row.p_path);
                  }
                }
              },
              "下载图片"
            );
          }
        },
        {
          title: "下载视频",
          key: "downloadVidio",
          width: "auto",
          align: "center",
          render: (h, params) => {
            if (params.row.v_path === "null" || params.row.v_path === "")
              return h("Span", {}, "无视频下载");
            else
              return h(
                "Button",
                {
                  props: {
                    type: "info",
                    size: "small"
                  },
                  on: {
                    click: () => {
                      // 下载视频代码
                      FileSaver.saveAs(params.row.v_path);
                    }
                  }
                },
                "下载视频"
              );
          }
        }
      ]
    };
  },
  computed: {
    ...mapState([
      "eventsQueryTableData",
      "eventsTotal",
      "eventsPageCurrent",
      "eventsPageSize"
    ]),
    showTableData() {
      //eventstable不同颜色表示不同报警类型
      return this.eventsQueryTableData.map(ele => {
        let cellClassName = "";
        if (ele.event === "非法停车") {
          cellClassName = "demo-table-info-cell-stop";
        } else if (ele.event === "行人入侵") {
          cellClassName = "demo-table-info-cell-person";
        } else if (ele.event === "道路缓行") {
          cellClassName = "demo-table-info-cell-slow";
        } else if (ele.event === "道路拥堵") {
          cellClassName = "demo-table-info-cell-jam";
        } else if (ele.event === "车辆逆行") {
          cellClassName = "demo-table-info-cell-back";
        } else if (ele.event === "非机动车") {
          cellClassName = "demo-table-info-cell-nonvehicle";
        }

        ele.cellClassName = {
          road_name: cellClassName,
          //datetime: cellClassName,
          event: cellClassName
          //: cellClassName
        };
        return ele;
      });
    },
    eventDetailTitle() {
      return (
        this.eventDetailData.road_name +
        " - " +
        this.eventDetailData.event +
        " - " +
        this.eventDetailData.datetime
      );
    }
  },
  methods: {
    ...mapActions(["updateEventsQueryPageIndex"]),
    handleTableClick(row, index) {
      this.eventDetailData = row;
      this.showPeopleImg();
      this.isShowModal = true;
      this.showImgVideo = true;
      this.showControlTime = false;
    },
    showPeopleImg() {
      if (this.eventDetailData.event == "行人入侵") {
        this.modelWidth = 1440;
        this.showPeople = true;
      } else {
        this.modelWidth = 1080;
        this.showPeople = false;
      }
    },
    showEventImg() {
      this.showPeopleImg();
      this.showImgVideo = true;
      this.showControlTime = false;
    },
    showEventVideo() {
      this.showPeople = false;
      this.modelWidth = 1080;
      this.showImgVideo = false;
      this.showControlTime = true;
    },
    handleRowChange(currentRow, oldCurrentRow) {
      // console.log('单击行：', currentRow, oldCurrentRow)
    },
    handlePageChange(page) {
      this.updateEventsQueryPageIndex(page);
    },
    toEventTime() {
      this.$refs.videoFlag.currentTime = 8;
    }
  }
};
</script>

<style lang="less">
@table-background-color: #0a4271 !important;

.events-table .ivu-table {
  color: #fff;
  font-size: 16px;
  background-color: rgba(1, 35, 124, 0.1) !important;
  //background-color: #01237c !important;
  overflow-x: hidden;
  overflow-y: hidden;
}
// 表格字体
.events-table .ivu-table td {
  font-size: 16px;
  font-weight: bold;
}
// 表格头的字体
.events-table .ivu-table th {
  background-color: @table-background-color;
  font-size: 18px;
  font-weight: bold;
  height: 42px;
}
.events-table .ivu-table-wrapper {
  border: 1px solid @table-background-color;
}
.events-table .ivu-table:after {
  background-color: @table-background-color;
}
.events-table .ivu-table td,
.ivu-table th {
  border-bottom: 1px solid @table-background-color;
}
// 停车
.events-table .ivu-table .demo-table-info-cell-stop {
  color: #faad14;
  font-weight: bold;
}
// 行人入侵
.events-table .ivu-table .demo-table-info-cell-person {
  color: #fff566;
  font-weight: bold;
}
// 车辆慢行
.events-table .ivu-table .demo-table-info-cell-slow {
  color: #5fb4ec;
  font-weight: bold;
}
// 车辆拥堵
.events-table .ivu-table .demo-table-info-cell-jam {
  color: #e9744d;
  font-weight: bold;
}
// 逆行
.events-table .ivu-table .demo-table-info-cell-back {
  color: #bb5e00;
  font-weight: bold;
}
// 非机动车
.events-table .ivu-table .demo-table-info-cell-nonvehicle {
  color: #64a600;
  font-weight: bold;
}
.events-table .ivu-table-row-hover td {
  color: #fff;
  background-color: #104ba3 !important;
  // background-color: #36cfc9 !important;
}
.events-table .ivu-table-row-highlight td {
  background-color: #40a9ff !important;
  // background-color: #36cfc9 !important;
}
// .events-table .ivu-table-stripe .ivu-table-body tr:nth-child(2n) td, .ivu-table-stripe .ivu-table-fixed-body tr:nth-child(2n) td{
//   background-color: #3A5683;
// }
.table-detail-xxx .ivu-icon-ios-close {
  font-size: 41px !important;
}
.table-detail-xxx .ivu-modal-body {
  padding: 2px 10px;
}

@table-background-color: #0a4271 !important;

.ivu-table {
  color: #fff !important;
  overflow-x: hidden !important;
  background-color: #01237c !important;
  overflow-y: hidden !important;
}
.ivu-table .demo-table-info-row td {
  font-size: 16px;
}
.ivu-table th {
  background-color: @table-background-color;
  font-size: 16px;
}
.ivu-table .demo-table-error-row td {
  background-color: #ff6600;
  color: #fff;
}
.ivu-table td.demo-table-info-column {
  background-color: #2db7f5;
  color: #fff;
}
.ivu-table .demo-table-info-cell-name {
  background-color: #2db7f5;
  color: #fff;
}
.ivu-table .demo-table-info-cell-age {
  background-color: #ff6600;
  color: #fff;
}
.ivu-table .demo-table-info-cell-address {
  background-color: #187;
  color: #fff;
}
.ivu-table-wrapper {
  border: 1px solid @table-background-color;
}
.ivu-table:after {
  background-color: @table-background-color;
}
.ivu-table td,
.ivu-table th {
  border-bottom: 1px solid @table-background-color;
}
.ivu-table .demo-table-info-row-legal {
  color: #18b566;
}
.ivu-table .demo-table-info-row-illegal {
  color: #f29100;
}
.ivu-table .demo-table-info-cell-legal {
  color: #18b566;
  font-weight: bold !important;
}
.ivu-table .demo-table-info-cell-illegal {
  color: #f29100;
  font-weight: bold !important;
}
.ivu-page-next,
.ivu-page-prev {
  background-color: #01237c !important;
}
.ivu-page-item {
  background-color: #1081b2 !important;
  color: red !important;
}
.ivu-page-item a {
  color: white !important;
}
.ivu-page-item-active a {
  color: blue !important;
}
.ivu-page-options-elevator input {
  background-color: #1081b2 !important;
  color: #fff !important;
}
.ivu-table td {
  background-color: #01237c !important;
}
.ivu-table-overflowX {
  overflow-x: hidden !important;
}
.ivu-table-tip table td {
  font-size: 16px;
}
.ivu-table-tip {
  overflow: hidden !important;
}

@background-color: #0a4271 !important;
@font-color: #fff !important;

.ivu-form-item-label {
  font-size: 1.6rem !important;
  color: @font-color;
}
.ivu-input {
  border: 1px solid @background-color;
  background-color: @background-color;
  font-size: 14px;
  font-weight: bold;
  color: #fff !important;
}
.ivu-select-selection {
  width: 180px;
  background-color: @background-color;
  border: 1px solid @background-color;
}
.ivu-select-placeholder {
  // color: #fff !important;
  font-size: 14px !important;
  font-weight: bold;
}
.ivu-select-dropdown {
  background-color: @background-color;
  margin: 0px 0px;
}
.ivu-select-item {
  color: #fff !important;
  font-size: 14px !important;
  font-weight: bold;
}
input::-webkit-input-placeholder {
  // color: #fff !important;
  font-size: 14px;
}
.ivu-tabs-bar {
  border-bottom: 2px solid @background-color;
}
.ivu-select-selected-value {
  color: #fff;
  font-size: 16px !important;
}
.ivu-select-item-focus {
  background-color: @background-color;
}
.ivu-select-item-selected,
.ivu-select-item-selected:hover {
  color: #52c41a !important;
}
.ivu-select-item:hover {
  background-color: #1081b3 !important;
}
.ivu-date-picker-cells-cell-selected em,
.ivu-date-picker-cells-cell-selected:hover em {
  // color: #52c41a !important;
  color: #1081b2 !important;
}
.ivu-date-picker-cells-cell-range:before {
  background-color: #1081b2 !important;
  // background-color: #52c41a !important;
}
.ivu-btn {
  font-size: 14px !important;
}
</style>
